package com.markyao.service;

import com.markyao.dao.mapper.OrderMapper;
import com.markyao.dao.pojo.Address;
import com.markyao.dao.pojo.Order;
import com.markyao.dao.pojo.OrderItem;
import com.markyao.dao.pojo.Product;
import com.markyao.vo.CartVo;
import com.markyao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AddressService addressService;
    @Autowired
    CartService cartService;

    @Transactional
    public Result addOrder(Integer aid, Integer uid, Integer[] cids, String username) {
        //购物车订单
        //1.首先要通过aid查找address信息，计算出总价,插入order表
        //2.通过cids查找出商品详情信息,将详情信息插入orderItem表,有几个cid就插入几个
        Address address=addressService.findByAid(uid,aid);
        Result byCids = cartService.findByCids(cids);
        List<CartVo> cartVos = (List<CartVo>) byCids.getData();
        Long total=0l;
        for (CartVo cartVo : cartVos) {
            Long price = cartVo.getPrice();
            total+=price;//计算总价
        }
        Order order=new Order();
        order.setOrderTime(new Date());
        order.setCreatedTime(new Date());
        order.setPayTime(new Date());
        order.setModifiedTime(new Date());
        order.setModifiedUser(username);
        order.setCreatedUser(username);
        order.setRecvName(username);
        order.setRecvAddress(address.getAddress());
        order.setRecvArea(address.getAreaName());
        order.setRecvCity(address.getCityName());
        order.setRecvProvince(address.getProvinceName());
        order.setSTATUS(0);
        order.setTotalPrice(total);
        order.setRecvPhone(address.getPhone());
        order.setUid(uid);
        orderMapper.inserOrder(order);
        Integer oid = order.getOid();
        for (CartVo cartVo : cartVos) {
            OrderItem orderItem=new OrderItem();
            orderItem.setCreatedTime(new Date());
            orderItem.setCreatedUser(username);
            orderItem.setModifiedUser(username);
            orderItem.setImage(cartVo.getImage());
            orderItem.setOid(oid);
            orderItem.setPid(cartVo.getPid());
            orderItem.setPrice(cartVo.getPrice());
            orderItem.setTitle(cartVo.getTitle());
            orderItem.setNum(cartVo.getNum());
            orderItem.setModifiedTime(new Date());
            orderMapper.insertOrderItem(orderItem);
        }
        return new Result(true,"创建订单成功",111,null);
    }


    @Autowired
    ProductService productService;
    @Transactional
    public void addMiaoshaOrder(Integer uid, Integer goodsId) {
        Order order=new Order();
        Product product = productService.getProductByid(goodsId);
            //简单设置一下
        order.setUid(uid).
                setOid(0).
                    setTotalPrice(product.getPrice()).
                    setOrderTime(new Date()).
                    setCreatedTime(new Date());
        orderMapper.inserOrder(order);
        OrderItem orderItem=new OrderItem();
        orderItem.
                setOid(order.getOid()).
                setTitle(product.getTitle()).
                setNum(1).
                setPrice(product.getPrice()).
                setPid(product.getId()).
                setCreatedTime(new Date());

        orderMapper.insertOrderItem(orderItem);
    }


}

